using System;
using UnityEngine;
using Weapons;

public class PlayerAttackState : PlayerAbilityState
{
    private readonly NewWeapon _weapon;

    private readonly int inputIndex;
    private float _lastAttackTime;
    private bool _setVelocity;
    private bool _shouldCheckFlip;

    private float _velocityToSet;
    private int _Xinput;

    public bool CanContinueAttack;


    public PlayerAttackState(Player player, PlayerSM stateMachine, PlayerData playerData, string animBoolName,
        Core core, NewWeapon weapon, CombatInputs input) : base(player, stateMachine, playerData, animBoolName, core)
    {
        _weapon = weapon;

        inputIndex = (int)input;

        _weapon.OnExit += ExitHandler;
        // this.ColliderVector2 = this._player.Collider.offset;
    }

    //��ײ�еĳ���������
    public Vector2 ColliderVector2 { get; }

    private void _weapon_OnExit()
    {
        throw new NotImplementedException();
    }

    public override void AnimationFinshTrigger()
    {
        base.AnimationFinshTrigger();

        _isAbilityDone = true;
    }

    public override void DoChecks()
    {
        base.DoChecks();
    }

    public override void Enter()
    {
        base.Enter();

        _setVelocity = false;

        if (_startTime >= _lastAttackTime + _playerData._maxContinueAttackWaitTime) CanContinueAttack = false;
        else CanContinueAttack = true;

        _weapon.Enter(); //MARKER����������ϵͳ�ı�־�ɽ��빥��״̬����������֮���Խ�����ϵͳ���˳�����Ϊ�¼������ǿ��ǵ�������ִ���ٶȺ�ʱ���в��
        //Ŀ�����ж�������
    }

    public override void Exit()
    {
        base.Exit();
        _lastAttackTime = Time.time;
    }


    public override void LogicUpdate()
    {
        base.LogicUpdate();

        _weapon.CurrentInput = _player._playerInputHander.AttackInputs[inputIndex];

        if (!_isExitingState)
        {
            _Xinput = LightInputManager.Instance.NormalInputX;
            if (_shouldCheckFlip) _core.Movement.CheckIfShouldFilp(_Xinput);

            if (_setVelocity) _core.Movement.SetVelocityX(_velocityToSet * _core.Movement.FacingDirection);
        }
    }

    public override void PhysicsUpdate()
    {
        base.PhysicsUpdate();
    }

    public void WeapoInitial(PlayerAttackState state)
    {
        //_weapon.Initialize();
    }

    public void SetPlayerVelocity(float velocity)
    {
        _core.Movement.SetVelocityX(velocity * _core.Movement.FacingDirection);

        _velocityToSet = velocity;
        _setVelocity = true;
    }

    public void SetFlipChecck(bool value)
    {
        _shouldCheckFlip = value;
    }

    private void ExitHandler()
    {
        AnimationFinshTrigger();
    }

    public Vector2 WeapoInitial() //��Player����ײ�и߶���Ϊ����������������������н鷽��,��ʱȡ��ʹ�ã����������ݲ���
    {
        return ColliderVector2;
    }
}